import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:wallpaper/components/appbar_base.dart';
import 'package:wallpaper/generated/l10n.dart';

class GradePixiv extends StatefulWidget {
  const GradePixiv({super.key});

  @override
  State<GradePixiv> createState() => _GradePixivState();
}

// ignore: camel_case_types
class r18Item {
  final String title;
  final int grade;
  const r18Item(this.title, this.grade);
}

class _GradePixivState extends State<GradePixiv> {
  int r18 = 0;
  void getR18() async {
    final prefs = await SharedPreferences.getInstance();
    setState(() {
      r18 = prefs.getInt('r18') ?? 0;
    });
  }

  @override
  void initState() {
    getR18();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    List<r18Item> localList = [
      const r18Item('性感', 0),
      const r18Item('r18', 1),
      const r18Item('全部', 2),
    ];
    return Scaffold(
      appBar: AppbarBase(title: S.of(context).grade),
      body: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Flex(
          direction: Axis.vertical,
          spacing: 8,
          children: [
            for (var item in localList)
              Material(
                  color: Theme.of(context).colorScheme.primaryContainer,
                  borderRadius: BorderRadius.circular(8),
                  clipBehavior: Clip.antiAlias,
                  child: ListTile(
                    title: Text(item.title),
                    // 右侧图标
                    trailing: r18 == item.grade
                        ? Icon(
                            Icons.check,
                            color: Theme.of(context).colorScheme.primary,
                          )
                        : null,
                    onTap: () {
                      setState(() {
                        r18 = item.grade;
                      });
                      final prefs = SharedPreferences.getInstance();
                      prefs.then((value) {
                        value.setInt('r18', r18);
                      });
                    },
                  )),
          ],
        ),
      ),
    );
  }
}
